package com.tjx.service.impl;

import com.tjx.entity.Employee;
import com.tjx.mapper.EmployeeMapper;
import com.tjx.service.IEmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 员工表 服务实现类
 * </p>
 *
 * @author tjx
 * @since 2025-10-29
 */
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 管理员登录
     */
    @Override
    public Employee loginManagement(Employee employee) {
        Employee selectEmployee = employeeMapper.selectById(1);
        if (!selectEmployee.getAccount().equals(employee.getAccount())) {
            selectEmployee.setAccount(null);
            return selectEmployee;
        }
        if (!selectEmployee.getPassword().equals(employee.getPassword())) {
            selectEmployee.setAccount(null);
            return selectEmployee;
        }
        return selectEmployee;
    }

    /**
     * 新增员工
     */
    @Override
    public Integer employeeAdd(Employee employee) {
        employee.setCreateDate(LocalDate.now());
        employee.setUpdateDate(LocalDate.now());
        employee.setIsDeleted(0);
        int row1 = employeeMapper.insert(employee);
        if (row1 > 0) {
            employee.setEmployeeId("employee" + employee.getId());
            int row2 = employeeMapper.updateById(employee);
            return row2;
        }
        return row1;
    }

    /**
     * 修改员工信息
     */
    @Override
    public Integer employeeUpdate(Employee employee) {
        employee.setUpdateDate(LocalDate.now());
        return employeeMapper.updateById(employee);
    }

    /**
     * 查询所有员工
     */
    @Override
    public List<Employee> employeeQueryAll() {
        return employeeMapper.selectList(null);
    }
}
